<?php

/**
 * Plugin roundresult
 * @author Marko Bischof <joomlabasic@gmail.com>
 * @copyright Copyright &copy; 2006, Marko Bischof http://leagueman.joomlabasic.com
 * @package com_leagueman
 */

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$_MAMBOTS->registerFunction( 'onPrepareContent', 'show_history' );

function show_history( $published, &$row, &$params, $page ) {

	$regex = "#{(leagueman_history)\s*(.*?)}#";
	
	if(!$published) {
		$row->text = preg_replace($regex, "", $row->text);
	} else {
		$row->text = preg_replace_callback($regex, "history_replacer", $row->text);
	}
	
	return true;
}

function history_replacer(&$matches){
	global $mainframe, $database, $Itemid;
	
	require_once $mainframe->getCfg("absolute_path") . '/includes/domit/xml_saxy_lite_parser.php';
	require_once $mainframe->getPath("class", "com_leagueman");
	
	$attribs = Saxy_Lite_Parser::parseAttributes($matches[2]);
	
	$t = intval($attribs["t"]);
	$c = intval(mosGetParam($_GET, "tm", 0));
	
	$team = LM::getLMTable("Team");
	if(!$team->load($t)){
		return "no team data available";
	}
	
	$database->setQuery("SELECT tm.teammap_id,t.team_id, t.team_name, s.season_name, l.league_name, p.project_id
						   FROM #__leagueman_teammap tm, #__leagueman_team t, #__leagueman_season s, #__leagueman_league l, #__leagueman_project p
						  WHERE tm.teammap_team = " . $database->Quote($t) . "
						    AND tm.teammap_team = t.team_id
							AND tm.teammap_project = p.project_id
							AND p.project_season = s.season_id
							AND p.project_league = l.league_id
					   ORDER BY s.ordering");
	
	$rows = $database->loadobjectList();
	
	ob_start();
		?>
		
		<table class="contentpane">
			<tr>
				<td class="sectiontableheader"><?php echo _LM_SEASON;?></td>
				<td class="sectiontableheader"><?php echo _LM_LEAGUE;?></td>
				<td class="sectiontableheader"><?php echo _LM_TEAM;?></td>
				<td class="sectiontableheader" align="center"><?php echo 'Fans';?></td>
			</tr>
			<?php 
			
				foreach($rows AS $row){
					
					$database->setQuery(
								sprintf(
									"SELECT COUNT(user) FROM jos_leagueman_fanbase
						  			  WHERE project = %s AND team = %s",
									$database->Quote($row->project_id),
									$database->Quote($row->team_id)
						  		)
					);
					
					//LM::dump($database->getQuery());
					$fans = $database->loadResult();
					
				?>
				
				<tr class="sectiontableentry1">
					<td><?php echo $row->season_name; ?></td>
					<td><?php echo $row->league_name; ?></td>
					<td>
						<a href="<?php echo sefRelToAbs("index.php?option=com_leagueman&amp;func=teaminfo&amp;tm={$row->teammap_id}&amp;Itemid=$Itemid");?>">
							<?php echo ($c == $row->teammap_id) ? "<b>" . $row->team_name . "</b>" : $row->team_name; ?>
						</a>
					</td>
					<td align="center"><?php echo $fans;?></td>
				</tr>
				
				<?php 
				
				} 
				
			?>
		</table>
		
		<?php
		$html = ob_get_contents();
	ob_end_clean();
	
	
	return $html;
}

?>
